package com.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: AaronChun
 * @create: 2019-03-21 21:48
 * @description:
 **/
public class DelegatePeople implements InvocationHandler {

    private Object target;

    public Object getInstance(Object target) {
        this.target = target;
        Class clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始检查年龄");
        System.out.println("-----------");
        System.out.println("proxy class:"+proxy.getClass());

        Object obj = method.invoke(target, args);

        System.out.println("-----------");
        System.out.println("年龄检查结束");

        return obj;
    }
}
